﻿using System;
using System.Collections.Generic;
using System.Linq;
using jQueryApi;

namespace TuringMachine
{
   public delegate void ButtonEventHandler(TMButton sender);


   public class TMButton : Span
   {
      jQueryObject m_button;

      static int counter = 0;
      bool m_bPressed;
      bool m_bEnabled;

      public event ButtonEventHandler OnClick;
      public TMButton(jQueryObject parent, string text, bool bRelative):
         base(parent,"button")
      {
         counter++;
         string suffix = counter + IdFactory.GetId();
         pObject.Append("<a href=\"#\" id=\"button" + suffix + "\" class=\"button_a\">" + text + "</a>");
         m_button = jQuery.Select("#button" + suffix);
         if (bRelative)
            pObject.CSS("position", "static");
         else
            pObject.CSS("position", "absolute");
         m_button.CSS("position", "static");

         m_button.Click(delegate(jQueryEvent ev)
         {
            if (OnClick != null && m_bEnabled)
               OnClick(this);
         });
         m_bEnabled = true;
      }

      public string pText
      {
         get { return m_button.GetText(); }
         set { m_button.Text(value); }
      }

      public bool pEnabled
      {
         get
         {
            return m_bEnabled;
         }
         set
         {
            if (value != m_bEnabled)
            {
               m_bEnabled = value;
               if (m_bEnabled)
                  m_button.RemoveClass("disabled");
               else
                  m_button.AddClass("disabled");
            }
         }
      }

      public Color pBackgroundColor
      {
         set
         {
            pObject.Children().CSS("background-color", value.ToString());
         }
         get
         {
            return new Color(0, 0, 0);
         }
      }

      public bool pPressed
      {
         get
         {
            return m_bPressed;
         }
         set
         {
            if (m_bPressed != value)
            {
               if (value)
                  m_button.AddClass("pressed");
               else
                  m_button.RemoveClass("pressed");
               m_bPressed = value;
            }
         }
      }

      public int GetWidth()
      {
         return m_button.GetWidth();
      }

   }


   public class TMSwitchButton : TMButton
   {
      public TMSwitchButton(jQueryObject parent, string text, bool bRelative) :
         base(parent, text, bRelative)
      {
         OnClick += new ButtonEventHandler(TMSwitchButton_OnClick);
      }

      void TMSwitchButton_OnClick(TMButton sender)
      {
         pPressed = !pPressed;
      }


   }
}
